package net.zoneland.knowledge.service.impl;

import net.zoneland.knowledge.controller.bean.SanHuiYiKeVo;
import net.zoneland.knowledge.dao.SanHuiYiKeMapper;
import net.zoneland.knowledge.enums.CmpDnEnum;
import net.zoneland.knowledge.enums.CompanyEnum;
import net.zoneland.knowledge.model.SanHuiYiKe;
import net.zoneland.knowledge.service.SanHuiYiKeService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 三会一课业务实现类.
 * @author zx
 */
@Service
public class SanHuiYiKeServiceImpl extends AbstractZhiXumServiceImpl implements SanHuiYiKeService {

    @Autowired
    private SanHuiYiKeMapper sanHuiYiKeMapper;

    @Override
    public List<SanHuiYiKe> searchByParam(final SanHuiYiKeVo sanHuiYiKeVo) {
        checkParam(sanHuiYiKeVo);
        sanHuiYiKeVo.setPage();
        return sanHuiYiKeMapper.search(sanHuiYiKeVo);
    }

    @Override
    public List<SanHuiYiKe> searchList(final SanHuiYiKeVo sanHuiYiKeVo) {
        checkParam(sanHuiYiKeVo);
        return sanHuiYiKeMapper.search(sanHuiYiKeVo);
    }

    @Override
    public List<SanHuiYiKe> searchNoMeetingsNameList(final SanHuiYiKeVo sanHuiYiKeVo) {
        Optional.ofNullable(sanHuiYiKeVo.getMeetingStartTime())
            .ifPresent(s -> sanHuiYiKeVo.setMeetingEndTime(Optional.ofNullable(sanHuiYiKeVo.getMeetingEndTime()).
                orElseGet(LocalDate::now)));
        meetingNameToList(sanHuiYiKeVo);
        checkOrgUuids(sanHuiYiKeVo);
        return sanHuiYiKeMapper.searchList(sanHuiYiKeVo);
    }

    @Override
    protected void checkParam(SanHuiYiKeVo sanHuiYiKeVo) {
        super.handlerParam(sanHuiYiKeVo);
        meetingNameToList(sanHuiYiKeVo);
        checkOrgUuids(sanHuiYiKeVo);
    }

    /**
     * 党组织如果存在，则传入当前登录用户的部门信息.
     * @param sanHuiYiKeVo 会议名称
     */
    protected void checkOrgUuids(final SanHuiYiKeVo sanHuiYiKeVo) {
        final Optional<List<String>> orgUuids = Optional.ofNullable(sanHuiYiKeVo.getOrgUuids())
                .filter(CollectionUtils::isNotEmpty);
        sanHuiYiKeVo.setOrgUuids(getNewOrgUuids(orgUuids));
        sanHuiYiKeVo.setCompanyName(getNewCompanyName(orgUuids, sanHuiYiKeVo));
    }

    /**
     * 去除党组织集合中为空的数据
     * @param orgUuids
     * @return
     */
    private List<String> getNewOrgUuids(final Optional<List<String>> orgUuids){
        return orgUuids
                .map(uuids -> uuids.stream()
                    .filter(StringUtils::isNotBlank)
                    .collect(Collectors.toList()))
                .orElse(Collections.emptyList());
    }

    /**
     * 获取公司名称
     * @param orgUuids
     * @param sanHuiYiKeVo
     * @return
     */
    private String getNewCompanyName(final Optional<List<String>> orgUuids, final SanHuiYiKeVo sanHuiYiKeVo){
        return orgUuids.map(strings -> sanHuiYiKeVo.getUser().getCompanyName())
                .orElseGet(() -> Optional.ofNullable(sanHuiYiKeVo.getCompanyName())
                        .filter(Predicate.isEqual(CompanyEnum.PROVINCE_PARTY.getName()))
                        .map(n -> CmpDnEnum.ZMCC.getName())
                        .orElse(sanHuiYiKeVo.getCompanyName()));
    }

    @Override
    public SanHuiYiKe getInfoById(final String id) {
        return sanHuiYiKeMapper.getInfoById(id);
    }
}
